const app = getApp()
Page({
  data: {
    //购物车数据
    cart: [
      {
        cart_id: 1, //商品ID
        name: '正宗越南白肉火龙果 约750g/份', //商品名称
        quantity: 1, //商品数量
        image: 'https://i.loli.net/2020/11/21/ZCzEay1DAwNtjU7.jpg', //商品图片
        price: 8.9, //商品价格
        checks: false //是否选中
      },
      {
        cart_id: 2, //商品ID
        name: '机蔬菜小青菜500g', //商品名称
        quantity: 1, //商品数量
        image: 'https://img.alicdn.com/imgextra/i1/3421077522/O1CN01kxv2v225R7ggAFSPb_!!0-item_pic.jpg_430x430q90.jpg', //商品图片
        price: 1.8, //商品价格
        checks: false //是否选中
      }
    ], 

    total_all_price: 0, //商品的总价格
    all_shop: 0, //所选商品的id
    shop_quantity: 0, //所选商品的数量
    good_one: [], //单个商品的数据
    checked: false, //是否为全选
    is_shop: false, //是否有商品
    shopData: null,
    cart: []
  },
  onShow: function() {
    var cart = wx.getStorageSync('cart');



    // 生命周期函数--监听页面显示
    var that = this;
    that.setData({
      total_all_price: 0,
      // checked: false
      cart: cart
    })
    var user_data = wx.getStorageSync('user_data');
  
  },

  // +-按钮被点击
  addButtonClick: function(tap) {
    // console.log(tap);
    const {operation,id} = tap.currentTarget.dataset
    let {cart} = this.data
    const index = parseInt(tap.currentTarget.id);

    if ( cart[index].sold_num == 1 && operation == -1) { //判断是否等于1
      var that = this
      wx.showModal({
        title: '提示',
        content: '确定 要移出购物车吗？',
        success: function(sm) {
          if (sm.confirm) {
            cart.splice(index,1)
            that.setData({cart})
          } else if (sm.cancel) {
           console.log('');
          }
        }
      })
    } else {
        cart[index].sold_num += operation
        cart.sold_num=cart[index].sold_num
        wx.setStorageSync('cart', cart);
        this.setData({cart});
    }


   
    

    
   
    // that.getTotalPrice(); //重新计算总价格
  },

  /*checkbox 选中或未选中都会触发该事件*/
  checkboxChange: function(e) {
    var that = this;
    that.setData({
      all_shop: e.detail.value,
    })
  },
  //删除单个商品
  deleteGoods(e) {
    const index = parseInt(e.currentTarget.id);
    // const cartId = e.currentTarget.dataset.cart_id;
    let {cart} = this.data
    wx.showModal({
      title: '温馨提示',
      content: '确定删除当前商品吗？',
      confirmColor: "#f00",
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中'
          });
          cart.splice(index,1)
          this.setData({cart})
          //更新总价
          this.getTotalPrice();
          wx.hideLoading();
         
        }
      }
    })
  },
  //更新数据函数
  updateCarts(data) {
    this.setData({
      cart: data
    })
  },
  /*点击结算 */
  toBuy: function(tap) {
    var that = this;
    let good = this.data.cart; //所有商品数据
    let user_info = wx.getStorageSync('user_data')
    if (that.data.total_all_price == 0) { //判断是否选择了商品，这里我是判断总价格
      wx.showToast({
        title: '请选择商品',
      })
    } else {
      this.data.good_one = []; //重置数组
      for (var i = 0; i < this.data.cart.length; i++) { //循环购物车中的商品
        if (good[i].checks == true) {
          var good_one1 = [good[i].cart_id, good[i].quantity];
          this.data.good_one.push(good_one1) //将数据添加到数组里边
        }
      }
    
    }
  },
  /**点击全选 */
  setChecked: function() {
    let checked = this.data.checked; //是否为全选状态
    checked = !checked; //改变状态
    let cart = this.data.cart;
    for (let i = 0; i < cart.length; i++) {
      cart[i].checks = checked; // 改变所有商品状态
    }
    this.setData({
      checked: checked, //更新全选状态
      cart: cart //更新所有商品的状态
    });
    this.getTotalPrice(); //重新获取总价格
  },
  /**点击单个多选框 */
  allCheckbox: function(tap) {
    var a = 0 //设置初始总价格
    var index = tap.currentTarget.dataset.index //获取索引值
    let good = this.data.cart //获取购物车列表
    const checks = good[index].checks; //获取当前商品的选中状态
    good[index].checks = !checks //改变当前商品的状态
    var all_shop_1 = parseInt(good[index].cart_id) //获取商品的购物车id
    var shop_quantity_1 = good[index].quantity //获取商品的数量
    this.setData({
      cart: good
    });
    this.getTotalPrice() //重新获取总价格
    /*设置全选 */
    for (let i = 0; i < good.length; i++) {
      a += good[i].sold_num * good[i].cor_price
    }
    if (this.data.total_all_price == a) {
      this.setData({
        checked: true
      })
    } else {
      this.setData({
        checked: false
      })
    }
  },
  /**计算总价格  */
  getTotalPrice() {
    let good = this.data.cart; // 获取购物车列表
    let total = 0;
    for (let i = 0; i < good.length; i++) { // 循环列表得到每个数据
      if (good[i].checks) { // 判断选中才会计算价格
        total += good[i].sold_num * good[i].cor_price; // 所有价格加起来
      }
    }
    this.setData({ // 最后赋值到data中渲染到页面
      good: good,
      total_all_price: total.toFixed(2),
    });
  },
})